package com.ute.chinesechess;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;

public class MainActivity extends Activity implements OnTouchListener,
		OnDragListener {

	int iTruoc, jTruoc;
	int iSau, jSau;
	int i, j;
	public static NguoiChoi[] NguoiChoi = new NguoiChoi[2];
	QuanCo qc;
	QuanCo sau;
	static int LuotDi = 0;
	int winner = 2;
	Button start, stop;
	TextView name1, name2, time1, time2;

	CountDownTimer count = new CountDownTimer(60000, 1000) {
		public void onTick(long millisUntilFinished) {
			// TODO Auto-generated method stub
			if (LuotDi == 0) {
				if (millisUntilFinished / 1000 <= 10)
					time1.setTextColor(Color.RED);
				else
					time1.setTextColor(Color.WHITE);
				time1.setVisibility(View.VISIBLE);
				time1.setText("" + millisUntilFinished / 1000);
				time2.setVisibility(View.INVISIBLE);
			} else {
				if (millisUntilFinished / 1000 <= 10)
					time2.setTextColor(Color.RED);
				else
					time2.setTextColor(Color.WHITE);
				time2.setVisibility(View.VISIBLE);
				time2.setText("" + millisUntilFinished / 1000);
				time1.setVisibility(View.INVISIBLE);
			}
		}

		public void onFinish() {
			// TODO Auto-generated method stub
			if (LuotDi == 0) {
				LuotDi = 1;
			} else {
				LuotDi = 0;
			}
			ThongBao("Hết giờ! Đổi lượt đi!");
			start();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		findView();
		start = (Button) findViewById(R.id.btnStart);
		name1 = (TextView) findViewById(R.id.name1);
		name2 = (TextView) findViewById(R.id.name2);
		time1 = (TextView) findViewById(R.id.time1);
		time2 = (TextView) findViewById(R.id.time2);
		stop = (Button) findViewById(R.id.btnDung);
		stop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});
		start.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				winner = -1;
				LuotDi = 0;
				ThongBao("Bắt đầu chơi!");
				count.start();
			}
		});
		// Neu mo game da luu thi khong khoi tao ntn!
		BanCo.KT();
		VanCo.KT();
		LuotDi = 0;
	}

	@Override
	public boolean onTouch(View v, MotionEvent e) {
		iTruoc = getToaDoHang((LinearLayout) v.getParent());
		jTruoc = getToaDoCot((LinearLayout) v.getParent());
		qc = BanCo.timQuanCo(iTruoc, jTruoc);

		if (winner != -1) {
			return false;
		}

		if (qc.Phe != LuotDi) {
			ThongBao("Chưa tới lượt đi");
			return false;
		}
		if (e.getAction() == MotionEvent.ACTION_DOWN) {
			DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
			v.startDrag(null, shadowBuilder, v, 0);
			v.setVisibility(View.INVISIBLE);
			return true;
		} else if (e.getAction() == MotionEvent.ACTION_UP) {
			v.setVisibility(View.VISIBLE);
			return true;
		} else
			return false;
	}

	@Override
	public boolean onDrag(View v, DragEvent e) {
		if (e.getAction() == DragEvent.ACTION_DROP) {

			View view = (View) e.getLocalState();
			ViewGroup from = (ViewGroup) view.getParent();
			LinearLayout to = (LinearLayout) v;

			iTruoc = getToaDoHang((LinearLayout) from);
			jTruoc = getToaDoCot((LinearLayout) from);
			iSau = getToaDoHang(to);
			jSau = getToaDoCot(to);
			qc = BanCo.timQuanCo(iTruoc, jTruoc);

			if (qc.Phe != LuotDi) {
				return false;
			}
			if (qc.Move(iSau, jSau) == true) {
				if (BanCo.board[iSau][jSau].Trong == true) {
					from.removeView(view);
					to.removeAllViewsInLayout();
					to.addView(view);
					BanCo.board[iSau][jSau].Phe = BanCo.board[iTruoc][jTruoc].Phe;
					BanCo.board[iSau][jSau].Trong = false;
					BanCo.board[iSau][jSau].ThuTu = BanCo.board[iTruoc][jTruoc].ThuTu;
					BanCo.board[iSau][jSau].Ten = BanCo.board[iTruoc][jTruoc].Ten;
					qc.setViTri(iSau, jSau);
					BanCo.board[iTruoc][jTruoc].reset();
					count.cancel();
					if (LuotDi == 0)
						LuotDi = 1;
					else
						LuotDi = 0;
					count.start();
					view.setVisibility(View.VISIBLE);
					if (qc.Phe == 0) {
						// Log.d("Tuong"+qc.Phe,""+VanCo.ChieuBi(1));
						if (VanCo.ChieuBi(1) == true) {
							ThongBaoCB(1);
							winner = 0;
							count.cancel();
							return false;
						}
						if (VanCo.TuongAnToan(1) == false)
							ThongBao("Chiếu Tướng quân Đen!");
					} else {
						// Log.d("Tuong"+qc.Phe,""+VanCo.ChieuBi(0));
						if (VanCo.ChieuBi(0) == true) {
							ThongBaoCB(0);
							winner = 1;
							count.cancel();
							return false;
						}
						if (VanCo.TuongAnToan(0) == false)
							ThongBao("Chiếu Tướng quân Đỏ!");
					}

				} else {
					if (BanCo.board[iSau][jSau].Phe != BanCo.board[iTruoc][jTruoc].Phe) {
						sau = BanCo.timQuanCo(iSau, jSau);
						sau.Song = false;
						from.removeView(view);
						to.removeAllViewsInLayout();
						to.addView(view);
						BanCo.board[iSau][jSau].Phe = BanCo.board[iTruoc][jTruoc].Phe;
						BanCo.board[iSau][jSau].Trong = false;
						BanCo.board[iSau][jSau].ThuTu = BanCo.board[iTruoc][jTruoc].ThuTu;
						BanCo.board[iSau][jSau].Ten = BanCo.board[iTruoc][jTruoc].Ten;
						qc.setViTri(iSau, jSau);
						BanCo.board[iTruoc][jTruoc].reset();
						view.setVisibility(View.VISIBLE);
						count.cancel();
						if (LuotDi == 0)
							LuotDi = 1;
						else
							LuotDi = 0;
						count.start();
						if (qc.Phe == 0) {
							// Log.d("Tuong"+qc.Phe,""+VanCo.ChieuBi(1));
							if (VanCo.ChieuBi(1) == true) {
								ThongBaoCB(1);
								winner = 0;
								count.cancel();
								return false;
							}
							if (VanCo.TuongAnToan(1) == false)
								ThongBao("Chiếu Tướng quân Đen!");
						} else {
							// Log.d("Tuong"+qc.Phe,""+VanCo.ChieuBi(0));
							if (VanCo.ChieuBi(0) == true) {
								ThongBaoCB(0);
								winner = 1;
								count.cancel();
								return false;
							}
							if (VanCo.TuongAnToan(0) == false)
								ThongBao("Chiếu Tướng quân Đỏ!");
						}
					} else {
						view.setVisibility(View.VISIBLE);
						return true;
					}
				}
			} // else
				// ThongBao("Bạn đi không đúng luật!");
			view.setVisibility(View.VISIBLE);
		}
		return true;
	}

	public void SaveGame(){
		for(int i=0;i<=9;i++){
			for(int j =0;j<=8;j++){
				if(BanCo.board[i][j].Trong == false){
					//Luu vao file
				}
			}
		}
	}
	
	public void LoadGame(){
		//Doc file
		//Gan vao ban co
		//Dieu chinh nguoi choi
		//Add view
		for(int i=0;i<=9;i++){
			for(int j =0;j<=8;j++){
				if(BanCo.board[i][j].Trong == false){
					//Luu vao file
				}
			}
		}
	}
	
	public void ThongBaoCB(int Phe) {
		AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
		if (Phe == 0)
			b.setMessage("Quân Đỏ bị chiếu bí!");
		else
			b.setMessage("Quân Đen bị chiếu bí!");
		b.setPositiveButton("Chơi lại", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
				Intent in = new Intent("com.ute.chinesechess.MAINACTIVITY");
				startActivity(in);
			}
		});
		b.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		b.create().show();
	}

	public void findView() {
		// Quan do
		findViewById(R.id.tuong1).setOnTouchListener(this);
		findViewById(R.id.sy11).setOnTouchListener(this);
		findViewById(R.id.sy12).setOnTouchListener(this);
		findViewById(R.id.tinh11).setOnTouchListener(this);
		findViewById(R.id.tinh12).setOnTouchListener(this);
		findViewById(R.id.xe11).setOnTouchListener(this);
		findViewById(R.id.xe12).setOnTouchListener(this);
		findViewById(R.id.phao11).setOnTouchListener(this);
		findViewById(R.id.phao12).setOnTouchListener(this);
		findViewById(R.id.ma11).setOnTouchListener(this);
		findViewById(R.id.ma12).setOnTouchListener(this);
		findViewById(R.id.chot11).setOnTouchListener(this);
		findViewById(R.id.chot12).setOnTouchListener(this);
		findViewById(R.id.chot13).setOnTouchListener(this);
		findViewById(R.id.chot14).setOnTouchListener(this);
		findViewById(R.id.chot15).setOnTouchListener(this);
		// Quan den
		findViewById(R.id.tuong2).setOnTouchListener(this);
		findViewById(R.id.sy21).setOnTouchListener(this);
		findViewById(R.id.sy22).setOnTouchListener(this);
		findViewById(R.id.tinh21).setOnTouchListener(this);
		findViewById(R.id.tinh22).setOnTouchListener(this);
		findViewById(R.id.xe21).setOnTouchListener(this);
		findViewById(R.id.xe22).setOnTouchListener(this);
		findViewById(R.id.phao21).setOnTouchListener(this);
		findViewById(R.id.phao22).setOnTouchListener(this);
		findViewById(R.id.ma21).setOnTouchListener(this);
		findViewById(R.id.ma22).setOnTouchListener(this);
		findViewById(R.id.chot21).setOnTouchListener(this);
		findViewById(R.id.chot22).setOnTouchListener(this);
		findViewById(R.id.chot23).setOnTouchListener(this);
		findViewById(R.id.chot24).setOnTouchListener(this);
		findViewById(R.id.chot25).setOnTouchListener(this);

		findViewById(R.id.ll1).setOnDragListener(this);
		findViewById(R.id.ll3).setOnDragListener(this);
		// Hang 0
		findViewById(R.id.o00).setOnDragListener(this);
		findViewById(R.id.o01).setOnDragListener(this);
		findViewById(R.id.o02).setOnDragListener(this);
		findViewById(R.id.o03).setOnDragListener(this);
		findViewById(R.id.o04).setOnDragListener(this);
		findViewById(R.id.o05).setOnDragListener(this);
		findViewById(R.id.o06).setOnDragListener(this);
		findViewById(R.id.o07).setOnDragListener(this);
		findViewById(R.id.o08).setOnDragListener(this);
		// Hang 1
		findViewById(R.id.o10).setOnDragListener(this);
		findViewById(R.id.o11).setOnDragListener(this);
		findViewById(R.id.o12).setOnDragListener(this);
		findViewById(R.id.o13).setOnDragListener(this);
		findViewById(R.id.o14).setOnDragListener(this);
		findViewById(R.id.o15).setOnDragListener(this);
		findViewById(R.id.o16).setOnDragListener(this);
		findViewById(R.id.o17).setOnDragListener(this);
		findViewById(R.id.o18).setOnDragListener(this);
		// Hang 2
		findViewById(R.id.o20).setOnDragListener(this);
		findViewById(R.id.o21).setOnDragListener(this);
		findViewById(R.id.o22).setOnDragListener(this);
		findViewById(R.id.o23).setOnDragListener(this);
		findViewById(R.id.o24).setOnDragListener(this);
		findViewById(R.id.o25).setOnDragListener(this);
		findViewById(R.id.o26).setOnDragListener(this);
		findViewById(R.id.o27).setOnDragListener(this);
		findViewById(R.id.o28).setOnDragListener(this);

		// Hang 3
		findViewById(R.id.o30).setOnDragListener(this);
		findViewById(R.id.o31).setOnDragListener(this);
		findViewById(R.id.o32).setOnDragListener(this);
		findViewById(R.id.o33).setOnDragListener(this);
		findViewById(R.id.o34).setOnDragListener(this);
		findViewById(R.id.o35).setOnDragListener(this);
		findViewById(R.id.o36).setOnDragListener(this);
		findViewById(R.id.o37).setOnDragListener(this);
		findViewById(R.id.o38).setOnDragListener(this);

		// Hang 4
		findViewById(R.id.o40).setOnDragListener(this);
		findViewById(R.id.o41).setOnDragListener(this);
		findViewById(R.id.o42).setOnDragListener(this);
		findViewById(R.id.o43).setOnDragListener(this);
		findViewById(R.id.o44).setOnDragListener(this);
		findViewById(R.id.o45).setOnDragListener(this);
		findViewById(R.id.o46).setOnDragListener(this);
		findViewById(R.id.o47).setOnDragListener(this);
		findViewById(R.id.o48).setOnDragListener(this);

		// Hang 5
		findViewById(R.id.o50).setOnDragListener(this);
		findViewById(R.id.o51).setOnDragListener(this);
		findViewById(R.id.o52).setOnDragListener(this);
		findViewById(R.id.o53).setOnDragListener(this);
		findViewById(R.id.o54).setOnDragListener(this);
		findViewById(R.id.o55).setOnDragListener(this);
		findViewById(R.id.o56).setOnDragListener(this);
		findViewById(R.id.o57).setOnDragListener(this);
		findViewById(R.id.o58).setOnDragListener(this);

		// Hang 6
		findViewById(R.id.o60).setOnDragListener(this);
		findViewById(R.id.o61).setOnDragListener(this);
		findViewById(R.id.o62).setOnDragListener(this);
		findViewById(R.id.o63).setOnDragListener(this);
		findViewById(R.id.o64).setOnDragListener(this);
		findViewById(R.id.o65).setOnDragListener(this);
		findViewById(R.id.o66).setOnDragListener(this);
		findViewById(R.id.o67).setOnDragListener(this);
		findViewById(R.id.o68).setOnDragListener(this);
		// Hang 7
		findViewById(R.id.o70).setOnDragListener(this);
		findViewById(R.id.o71).setOnDragListener(this);
		findViewById(R.id.o72).setOnDragListener(this);
		findViewById(R.id.o73).setOnDragListener(this);
		findViewById(R.id.o74).setOnDragListener(this);
		findViewById(R.id.o75).setOnDragListener(this);
		findViewById(R.id.o76).setOnDragListener(this);
		findViewById(R.id.o77).setOnDragListener(this);
		findViewById(R.id.o78).setOnDragListener(this);
		// Hang 8
		findViewById(R.id.o80).setOnDragListener(this);
		findViewById(R.id.o81).setOnDragListener(this);
		findViewById(R.id.o82).setOnDragListener(this);
		findViewById(R.id.o83).setOnDragListener(this);
		findViewById(R.id.o84).setOnDragListener(this);
		findViewById(R.id.o85).setOnDragListener(this);
		findViewById(R.id.o86).setOnDragListener(this);
		findViewById(R.id.o87).setOnDragListener(this);
		findViewById(R.id.o88).setOnDragListener(this);
		// Hang 9
		findViewById(R.id.o90).setOnDragListener(this);
		findViewById(R.id.o91).setOnDragListener(this);
		findViewById(R.id.o92).setOnDragListener(this);
		findViewById(R.id.o93).setOnDragListener(this);
		findViewById(R.id.o94).setOnDragListener(this);
		findViewById(R.id.o95).setOnDragListener(this);
		findViewById(R.id.o96).setOnDragListener(this);
		findViewById(R.id.o97).setOnDragListener(this);
		findViewById(R.id.o98).setOnDragListener(this);
	}

	public void veLogBanCo() {
		for (int i = 0; i < 10; i++) {
			Log.d("Hang" + i, BanCo.board[i][0].Phe + " "
					+ BanCo.board[i][1].Phe + " " + BanCo.board[i][2].Phe + " "
					+ BanCo.board[i][3].Phe + " " + BanCo.board[i][4].Phe + " "
					+ BanCo.board[i][5].Phe + " " + BanCo.board[i][6].Phe + " "
					+ BanCo.board[i][7].Phe + " " + BanCo.board[i][8].Phe);
		}
	}

	public void ThongBao(String s) {
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.thongbao,
				(ViewGroup) findViewById(R.id.toast_layout_root));
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(s);

		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(layout);
		toast.show();
	}

	public int getToaDoHang(LinearLayout ll) {
		int id = ll.getId();
		int i = -1;
		switch (id) {
		case R.id.o00:
		case R.id.o01:
		case R.id.o02:
		case R.id.o03:
		case R.id.o04:
		case R.id.o05:
		case R.id.o06:
		case R.id.o07:
		case R.id.o08:
			i = 0;
			break;
		case R.id.o10:
		case R.id.o11:
		case R.id.o12:
		case R.id.o13:
		case R.id.o14:
		case R.id.o15:
		case R.id.o16:
		case R.id.o17:
		case R.id.o18:
			i = 1;
			break;
		case R.id.o20:
		case R.id.o21:
		case R.id.o22:
		case R.id.o23:
		case R.id.o24:
		case R.id.o25:
		case R.id.o26:
		case R.id.o27:
		case R.id.o28:
			i = 2;
			break;
		case R.id.o30:
		case R.id.o31:
		case R.id.o32:
		case R.id.o33:
		case R.id.o34:
		case R.id.o35:
		case R.id.o36:
		case R.id.o37:
		case R.id.o38:
			i = 3;
			break;
		case R.id.o40:
		case R.id.o41:
		case R.id.o42:
		case R.id.o43:
		case R.id.o44:
		case R.id.o45:
		case R.id.o46:
		case R.id.o47:
		case R.id.o48:
			i = 4;
			break;
		case R.id.o50:
		case R.id.o51:
		case R.id.o52:
		case R.id.o53:
		case R.id.o54:
		case R.id.o55:
		case R.id.o56:
		case R.id.o57:
		case R.id.o58:
			i = 5;
			break;
		case R.id.o60:
		case R.id.o61:
		case R.id.o62:
		case R.id.o63:
		case R.id.o64:
		case R.id.o65:
		case R.id.o66:
		case R.id.o67:
		case R.id.o68:
			i = 6;
			break;
		case R.id.o70:
		case R.id.o71:
		case R.id.o72:
		case R.id.o73:
		case R.id.o74:
		case R.id.o75:
		case R.id.o76:
		case R.id.o77:
		case R.id.o78:
			i = 7;
			break;
		case R.id.o80:
		case R.id.o81:
		case R.id.o82:
		case R.id.o83:
		case R.id.o84:
		case R.id.o85:
		case R.id.o86:
		case R.id.o87:
		case R.id.o88:
			i = 8;
			break;
		case R.id.o90:
		case R.id.o91:
		case R.id.o92:
		case R.id.o93:
		case R.id.o94:
		case R.id.o95:
		case R.id.o96:
		case R.id.o97:
		case R.id.o98:
			i = 9;
			break;
		}
		return i;
	}

	public int getToaDoCot(LinearLayout ll) {
		int id = ll.getId();
		int j = -1;
		switch (id) {
		case R.id.o00:
		case R.id.o10:
		case R.id.o20:
		case R.id.o30:
		case R.id.o40:
		case R.id.o50:
		case R.id.o60:
		case R.id.o70:
		case R.id.o80:
		case R.id.o90:
			j = 0;
			break;
		case R.id.o01:
		case R.id.o11:
		case R.id.o21:
		case R.id.o31:
		case R.id.o41:
		case R.id.o51:
		case R.id.o61:
		case R.id.o71:
		case R.id.o81:
		case R.id.o91:
			j = 1;
			break;
		case R.id.o02:
		case R.id.o12:
		case R.id.o22:
		case R.id.o32:
		case R.id.o42:
		case R.id.o52:
		case R.id.o62:
		case R.id.o72:
		case R.id.o82:
		case R.id.o92:
			j = 2;
			break;
		case R.id.o03:
		case R.id.o13:
		case R.id.o23:
		case R.id.o33:
		case R.id.o43:
		case R.id.o53:
		case R.id.o63:
		case R.id.o73:
		case R.id.o83:
		case R.id.o93:
			j = 3;
			break;
		case R.id.o04:
		case R.id.o14:
		case R.id.o24:
		case R.id.o34:
		case R.id.o44:
		case R.id.o54:
		case R.id.o64:
		case R.id.o74:
		case R.id.o84:
		case R.id.o94:
			j = 4;
			break;
		case R.id.o05:
		case R.id.o15:
		case R.id.o25:
		case R.id.o35:
		case R.id.o45:
		case R.id.o55:
		case R.id.o65:
		case R.id.o75:
		case R.id.o85:
		case R.id.o95:
			j = 5;
			break;
		case R.id.o06:
		case R.id.o16:
		case R.id.o26:
		case R.id.o36:
		case R.id.o46:
		case R.id.o56:
		case R.id.o66:
		case R.id.o76:
		case R.id.o86:
		case R.id.o96:
			j = 6;
			break;
		case R.id.o07:
		case R.id.o17:
		case R.id.o27:
		case R.id.o37:
		case R.id.o47:
		case R.id.o57:
		case R.id.o67:
		case R.id.o77:
		case R.id.o87:
		case R.id.o97:
			j = 7;
			break;
		case R.id.o08:
		case R.id.o18:
		case R.id.o28:
		case R.id.o38:
		case R.id.o48:
		case R.id.o58:
		case R.id.o68:
		case R.id.o78:
		case R.id.o88:
		case R.id.o98:
			j = 8;
			break;
		}
		return j;
	}
}